package com.atguigu.spring.aop.jdbc;

import com.atguigu.spring.aop.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wk
 * @create 2021-06-03 14:23
 */
@ContextConfiguration(locations = "classpath:spring-jdbctemplate.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class JdbcTemplateTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testUpdate() {
        String sql = "insert into t_user values(null,?,?,?,?)";
        jdbcTemplate.update(sql,"卡特","5678",33,"男");
    }
    @Test
    public void testSelectById() {
        String sql = "select * from t_user where id=7";
        //query(sql, new BeanPropertyRowMapper<>(User.class))
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        list.forEach(user -> System.out.println(user));
    }
    @Test
    public void testSelectAll() {
        String sql = "select * from t_user ";
        //query(sql, new BeanPropertyRowMapper<>(User.class))
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        list.forEach(user -> System.out.println(user));
    }
    @Test
    public void testGetCount(){
        String sql="select count(id) from t_user ";
        System.out.println(jdbcTemplate.queryForObject(sql, Integer.class));
    }
    @Test
    public void testBatchInsert(){
        String sql="insert into t_user values(null,?,?,?,?)";
        List<Object[]>list=new ArrayList<>();
        list.add(new Object[]{"克劳福德","2345",23,"男"});
        list.add(new Object[]{"霍福德","2645",23,"男"});
        list.add(new Object[]{"克劳福德","986",23,"男"});
        jdbcTemplate.batchUpdate(sql,list);
    }
    @Test
    public void testBatchDelete(){
        String sql="delete from t_user where id in(?)";
        List<Object[]> list=new ArrayList<>();
        list.add(new Object[]{15});
        list.add(new Object[]{16});
        list.add(new Object[]{17});
        jdbcTemplate.batchUpdate(sql,list);

    }


}
